import Vue from 'vue'
import Vuex from 'vuex'
import Base64 from 'js-base64';
var base = Base64.Base64;
Vue.use(Vuex)
const storeMaker = (state) => {
  // 初始化
  Object.keys(state).map((key) => {
    if (key != 'nocaches') {
      // 判断类型获取本地存储数据
      if (typeof state[key] === 'object') {
        if (sessionStorage.getItem(key) && JSON.parse(sessionStorage.getItem(key))) {
          state[key] = JSON.parse(sessionStorage.getItem(key))
        }
      } else if (typeof state[key] === 'number') {
        if (sessionStorage.getItem(key) && parseInt(sessionStorage.getItem(key))) {
          state[key] = parseInt(sessionStorage.getItem(key))
        }
      } else {
        if (sessionStorage.getItem(key)) {
          if (sessionStorage.getItem(key) == 'true') {
            state[key] = true
          } else if (sessionStorage.getItem(key) == 'false') {
            state[key] = false
          } else {
            state[key] = sessionStorage.getItem(key)
          }

        }
      }
    }
  })
  Object.keys(state).map((key) => {

    if (key != 'nocaches') {
      // 判断类型获取本地存储数据
      if (typeof state[key] === 'object') {
        if (localStorage.getItem(key) && JSON.parse(localStorage.getItem(key))) {
          state[key] = JSON.parse(localStorage.getItem(key))
        }
      } else if (typeof state[key] === 'number') {
        if (localStorage.getItem(key) && parseInt(localStorage.getItem(key))) {
          state[key] = parseInt(localStorage.getItem(key))
        }
      } else {
        if (localStorage.getItem(key)) {
          state[key] = localStorage.getItem(key)
        }
      }
    }
  })
  // 重写set处理
  return new Proxy(state, {
    set: function (target, key, value) {
      let temp = value;
      if (typeof temp === 'object') {
        temp = JSON.stringify(temp)
      }
      if (key == 'loginInforXl') {
        localStorage.setItem(key, temp)
      } else {
        sessionStorage.setItem(key, temp)
      }
      return Reflect.set(target, key, value)
    }
  })
}
let removeByValue = (arr, val) => {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == val) {
      arr.splice(i, 1);
      break;
    }
  }
  return arr
}

let maskaddPrice = (val, id, flag) => {
  let temp = [];
  if (flag == 'add') {
    let arr = [...new Set([...val, ...[id]])]
    return arr
  } else {
    return removeByValue(val, id);
  }
}

//删除数组空元素
let delArrys = (arr, val) => {
  let tempArr = []
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] !== undefined && arr[i] != "") {
      tempArr.push(arr[i])
    }
  }
  console.log(1113, tempArr)
  return tempArr;
}
//返回登录进来获取的自选价格字符窜
let setLoginmyPrice = (str) => {
  let arr = []
  console.log(11144, typeof str)
  if (str) {
    if (typeof str == 'string') {
      arr = str.split(',')
      arr = delArrys(arr, '')
    }
  }
  return arr
}

/**
 * 注意这个数据在，注销以后一定要更改，否则注销会保留数据
 * 页面不会变化
 * 注销不需要更改的数据有indexData，startPage
 * 其余数据要在muinit里面回复
 *  */
const state = {
  loginInforXl: {}, //登陆成功获取的信息
  startPage: false, //是否经过启动页
  mymenu: {
    "tcl": 1,
    "dc": 1,
    "dlg": 1
  }, //1表示显示，0表示不显示
  indexData: {},
  pricepartkw: "", //价格分类关键词
  islogin: false,
  nocaches: {
    mymenuShow: false, //保存不需要缓存的数据
    loginmoduleshow: false,
    refresh: false,
    notMemberobj: {
      notMember: false, //是否是该频道会员
      notMembershow: false
    },
    kefuphone: "02150184981",
    replayformShow: {
      show: false,
      type: 0,
      text: "发布供应"
    },
    replaypartObj: {
      type: 1,
      serch: ""
    },
    pricepartObj: {
      kname: "",
      specs: ""
    }, //价格页面的分类和品目参数
  }
}


const mutations = {
  muinit(state, val) {
    state.loginInforXl = {} //登陆成功获取的信息
    state.mymenu = {
      "tcl": 1,
      "dc": 1,
      "dlg": 1
    }
    state.pricepartkw = "" //价格分类关键词
    state.islogin = false,
      state.nocaches = {
        loginmoduleshow: false,
        kefuphone: "02150184981",
        mymenuShow: false, //保存不需要缓存的数据
        refresh: false,
        notMemberobj: {
          notMember: false, //是否是该频道会员
          notMembershow: false
        },
        replayformShow: {
          show: false,
          type: 0,
          text: "发布供应"
        },
        replaypartObj: {
          type: 1,
          serch: ""
        },
        pricepartObj: {
          kname: "",
          specs: ""
        }, //价格页面的分类和品目参数
      }
  },
  munotMember(state, val) {
    state.nocaches.notMemberobj = val;
  },
  muIndexData(state, val) {
    state.indexData = val;
  },
  muloginmoduleshow(state, val) {
    state.nocaches.loginmoduleshow = val
  },
  muLoginInfor(state, val) {
    console.log("val", val)
    if (val.mymenu) {
      if (typeof val.mymenu == 'string') {
        val.mymenu = JSON.parse(val.mymenu);
      }
      state.mymenu = val.mymenu
    }
    if (val.tcl_myprice) {
      val.tcl_myprice = setLoginmyPrice(val.tcl_myprice)
    } else {
      val.tcl_myprice = []
    }
    if (val.dc_myprice) {
      val.dc_myprice = setLoginmyPrice(val.dc_myprice)
    } else {
      val.dc_myprice = []
    }
    if (val.dlg_myprice) {
      val.dlg_myprice = setLoginmyPrice(val.dlg_myprice)
    } else {
      val.dlg_myprice = []
    }
    state.loginInforXl = val;
    state.islogin = true
  },
  muislogin(state, val) {
    state.islogin = val;
  },
  //登录后操作价钱，自选和删除自选，对应操作local
  muLoginprice(state, val) {
    let str = '';
    if (val.channel == 'tcl') {
      str = state.loginInforXl.tcl_myprice
      let logininfo = state.loginInforXl; //原因是写入local的方法只能监听到loginInforXl
      logininfo.tcl_myprice = maskaddPrice(str, val.id, val.op);
      state.loginInforXl = logininfo
    } else if (val.channel == 'dc') {
      str = state.loginInforXl.dc_myprice
      let logininfo = state.loginInforXl
      logininfo.dc_myprice = maskaddPrice(str, val.id, val.op);
      state.loginInforXl = logininfo
    } else {
      str = state.loginInforXl.dlg_myprice
      let logininfo = state.loginInforXl
      logininfo.dlg_myprice = maskaddPrice(str, val.id, val.op);
      state.loginInforXl = logininfo
    }
  },
  muacmymenu(state, val) {
    state.mymenu = val;
    if (state.islogin) {
      let logininfo = state.loginInforXl; //原因是写入local的方法只能监听到loginInforXl
      logininfo.mymenu = val
      state.loginInforXl = logininfo
    }
  },
  muNomymenuShow(state, val) {
    state.nocaches.mymenuShow = val
  },
  mureplayformShow(state, val) {
    state.nocaches.replayformShow = val
  },
  mupricepartObj(state, val) {
    state.nocaches.pricepartObj = Object.assign({}, val)
  },
  mustartPage(state, val) {
    state.startPage = true
  },
  mupricepartkw(state, val) {
    state.pricepartkw = val
  },
  mureplaypartType(state, val) {
    state.nocaches.replaypartObj.type = val
  },
  muserch(state, val) {
    state.nocaches.replaypartObj.serch = val
  },
  murefresh(state, val) {
    state.nocaches.refresh = val
  },
}
const actions = {
  //保存登录信息
  acLoginInfor(ctx, val) {
    ctx.commit("muLoginInfor", val)
  },
  //验证token是否失效
  acislogin(ctx, val) {
    ctx.commit("muislogin", val)

  },
  //保存首页数据
  acIndexData(ctx, val) {
    ctx.commit("muIndexData", val)
  },
  //保存登陆进来选择的菜单
  acmymenu(ctx, val) {
    ctx.commit("muacmymenu", val)
  },
  //是否显示选择的菜单这个模块
  acNomymenuShow(ctx, val) {
    ctx.commit("muNomymenuShow", val)
  },
  acreplayformShow(ctx, val) {
    ctx.commit("mureplayformShow", val)
  },
  acpricepartObj(ctx, val) {
    ctx.commit("mupricepartObj", val)
  },
  acstartPage(ctx, val) {
    ctx.commit("mustartPage", val)
  },
  acpricepartkw(ctx, val) {
    console.log("val,val", val)
    ctx.commit("mupricepartkw", val)
  },
  acreplaypartType(ctx, val) {
    ctx.commit("mureplaypartType", val)
  },
  acserch(ctx, val) {
    ctx.commit("muserch", val)
  },
  acrefresh(ctx, val) {
    ctx.commit("murefresh", val)
  },
  //获取登录进来的自选价格列表shezhi
  acLoginprice(ctx, val) {
    ctx.commit("muLoginprice", val)
  },
  acinit(ctx) {
    ctx.commit("muinit")
  },
}
export default new Vuex.Store({
  state: storeMaker(state),
  actions,
  mutations
})
